<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>编辑数据字典</title>
  <link rel="stylesheet" href="/static/admin/css/common.css" media="all"/>
  <link rel="stylesheet" href="/static/common/layui/css/layui.css" media="all"/>
</head>
<body style="padding: 10px;">
<div class="site-block site-text">
  <form class="layui-form" action="">
    <div class="layui-form-item">
      <label class="layui-form-label">编码</label>
      <div class="layui-input-block">
        <input type="text" name="code" required="" lay-verify="required" placeholder="请输入编码"
               autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">名称</label>
      <div class="layui-input-block">
        <input type="text" name="text" required="" lay-verify="required" placeholder="请输入名称"
               autocomplete="off" class="layui-input">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">类型</label>
      <div class="layui-input-block">
        <input type="text" name="type" required="" lay-verify="required" placeholder="请输入类型"
               autocomplete="off" class="layui-input">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">状态</label>
      <div class="layui-input-block">
        <input type="radio" name="enable" value="1" title="启用">
        <input type="radio" name="enable" value="0" title="禁用" checked>
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">可否编辑</label>
      <div class="layui-input-block">
        <input type="checkbox" name="editable" value="1" lay-skin="switch" lay-text="开启|关闭">
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">顺序</label>
      <div class="layui-input-block">
        <input type="text" name="sortNo" required="" lay-verify="required|number" placeholder="请输入顺序值"
               autocomplete="off" class="layui-input">
      </div>
    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">备注</label>
      <div class="layui-input-block">
        <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
      </div>
    </div>
    <input type="hidden" name="id">
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formSub">保存</button>
      </div>
    </div>
  </form>
</div>

<script src="/static/common/layui/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/config.js"></script>
<script>
  layui.use(['form','jquery','customUtil'], function(){
    var form = layui.form,
        $ = layui.$,
        customUtil = layui.customUtil;

    var id = customUtil.getQueryString('id');
    if(!customUtil.isEmpty(id)){
      $.ajax({
        url:"/admin/sysDic/findById",
        data:{id:id},
        dataType:'json',
        success:function(result){
          if(result.success){
            var data = result.data;

            $('.layui-form').find('input:text').each(function () {
              var attrName = $(this).attr('name');
              $(this).val(data[""+attrName+""]);
            })

            if(data.enable){
              $("input:radio[value='"+data.enable+"']").attr('checked','true');
            }
            if(data.editable){
              $("input:checkbox").attr('checked','true');
            }
            $('textarea').val(data.remark)
            debugger;
            form.render();
          }
        }
      })
    }

    //监听提交
    form.on('submit(formSub)', function(data){
      $.ajax({
        url:'/admin/sysDic/save',
        type:'post',
        data:JSON.stringify(data.field),
        contentType:'application/json',
        dataType:'json',
        success:function (result) {
          if(result.success){
            layer.msg("保存成功!", function() {
              var index = parent.layer
              .getFrameIndex(window.name); //先得到当前iframe层的索引
              parent.layer.close(index); //再执行关闭
            })
          }else{
            layer.msg(result.message)
          }
        }

      })
      return false;
    });
  });
</script>
</body>
</html>